import { loginByUsername, logout, getUserInfo, getuserxinxin } from '@/api/login'
import { setStore, getStore, removeStore, clearStore } from '@/utils/auth'
import { Message } from 'element-ui'

const user = {
  state: {
    user: '',
    status: '',
    code: getStore('code'),
    token: getStore('Admin-Token'),
    name: '',
    disc: getStore('shangPingId'),
    tenantId: getStore('tenantId'),
    storeCode: getStore('storeCode'),
    buyerCode: getStore('buyerCode'),
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    },
    needid: getStore('needid'),
    userimg: getStore('userimg'),
    userid: getStore('userid'),
    choiceid: getStore('choiceid'),
    kehuid: getStore('kehuid'), // 客户列表里面编辑详情
    dingdanid: getStore('dingdanid') // 订单列表里面进入到订单详情
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
      setStore('code', state.code)
    },
    SET_TOKEN: (state, token) => {
      state.token = token
      setStore('Admin-Token', state.token)
    },
    SET_TENANTID: (state, tenantId) => {
      state.tenantId = tenantId
      setStore('tenantId', state.tenantId)
    },
    SET_STORECODE: (state, storeCode) => {
      state.storeCode = storeCode
      setStore('storeCode', state.storeCode)
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_DISC: (state, disc) => {
      state.disc = disc
      setStore('shangPingId', state.disc)
    },
    SET_CHOICEID: (state, choiceId) => {
      state.choiceid = choiceId
      setStore('choiceid', state.choiceid)
    },
    SET_KEHUID: (state, kehuid) => {
      state.kehuid = kehuid
      setStore('kehuid', state.kehuid)
    },
    SET_DINGDANID: (state, dingdanid) => {
      state.dingdanid = dingdanid
      setStore('dingdanid', state.dingdanid)
    },
    NEED_ID: (state, needid) => {
      state.needid = needid
      // console.log(state)
      // console.log(needid)
      setStore('needid', state.needid)
    },
    USER_IMG: (state, userimg) => {
      state.userimg = userimg
      console.log(state)
      console.log(userimg)
      setStore('userimg', state.userimg)
    },
    USER_ID: (state, userid) => {
      state.userid = userid
      setStore('userid', state.userid)
    },
    BUYER_CODE: (state, buyerCode) => {
      state.buyerCode = buyerCode
      setStore('buyerCode', state.buyerCode)
    }
  },

  actions: {
    // 用户名登录
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      // const username = userInfo.account.trim()
      console.log(commit)
      const phone = userInfo.phone.trim()
      const verificationCode = userInfo.verificationCode.trim()
      return new Promise((resolve, reject) => {
        // loginByUsername(username, userInfo.password).then(response => {
        loginByUsername(phone, verificationCode).then(response => {
          if (response.data.code === 0) {
            const data = response.data.data
            commit('SET_TOKEN', data.token)
            commit('SET_CODE', data.code)
            commit('USER_ID', data.adminUser.id)
            commit('USER_IMG', data.adminUser.headImg)
            resolve(response)
          } else if (response.data.code === -1) {
            // alert(2)
            Message({
              message: '登陆失败',
              type: 'error'
            })
            resolve(response)
            clearStore()
          } else {
            // alert(1);
            Message({
              message: response.data.message,
              type: 'error'
            })
            resolve(response)
            clearStore()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetuserDetail({ commit }) {
      return new Promise((resolve, reject) => {
        getuserxinxin().then(response => {
          const data = response.data.data
          commit('SET_TENANTID', data.id)
          commit('SET_STORECODE', data.storeCode)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户资源
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          if (!response.data) { // 由于mockjs 不支持自定义状态码只能这样hack
            reject('error')
          }
          const data = response.data.data
          // commit('SET_Router', data)
          // 下面这些 头像 名字  暂时没有   先把路由存在roles 里面
          commit('SET_ROLES', 'rlwb2')
          // commit('SET_NAME', data.name)
          // commit('SET_AVATAR', data.avatar)
          // commit('SET_INTRODUCTION','')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 新建订单的时候   存用户buyerCode

    // //修改用户名的时候
    buyerCode({ commit }, buyerCode) {
      commit('BUYER_CODE', buyerCode)
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_CODE', '')
          commit('SET_TENANTID', '')
          commit('SET_STORECODE', '')
          removeStore('Admin-Token')
          clearStore()
          resolve(resolve)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeStore('Admin-Token')
        clearStore()
        resolve()
      })
    }

  }
}

export default user
